/******************************************************************************/
/***                                                                        ***/
/***  SPscheme                                                              ***/
/***                                                                        ***/
/***  Module of the !SPExample program, of the "Software Protection Scheme" ***/
/***                                                                        ***/
/***  Contains the main control processes involved in creating the          ***/
/***  software protection parts of the code. It has been separated from the ***/
/***  main program for clarity of demonstation the protection code.         ***/
/***                                                                        ***/
/***                                                                        ***/
/***                                                                        ***/
/***  Written by N.Critchell, Acorn Computers                   April 1992  ***/
/***                                                                        ***/
/******************************************************************************/

 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>

 #include "kernel.h"
 #include "werr.h"

 #include "SProutines.h"
 #include "SPdefs.h" 






 char Reg_Number[17];
 char LicenceHolder[40];
 char LicenceType[16];






/******************************************************************************/
/***                                                                        ***/
/***  SoftwareProtectionScheme()                                            ***/
/***                                                                        ***/
/***  This routine controls the entire software protection schemes          ***/
/***  implementation. This routine will return if the software is either    ***/
/***  running legally or the user wishes to use it illegally. Otherwise     ***/
/***  the program is quit using the exit(0).                                ***/
/***                                                                        ***/
/***  Returns:                                                              ***/
/***     FALSE, if running legally                                          ***/
/***     TRUE, if running illegally, so that an additional bar icon can be  ***/
/***           installed.                                                   ***/
/******************************************************************************/

int SoftwareProtectionScheme(int *SP_win_handle)
    {
    REG_NO reg;
    extern char LicenceType[];
    int status;
    int ret_value = FALSE; 
#if NET_LICENCE == TRUE
    extern int *window_1;
    extern int *window_2;
    extern int *window_3;
    char FS_Name[32];
#endif

#if SINGLE_LICENCE == TRUE
      strcpy(LicenceType, "Single User");
#else
 #if NET_LICENCE == TRUE
  #if SITE_LICENCE == FALSE
      strcpy(LicenceType, "Network");
  #else
      strcpy(LicenceType, "Site & Network");
  #endif
 #else
  #if MULTI_LICENCES == -1
      strcpy(LicenceType, "Site");
  #else
      strcpy(LicenceType, "Limited Site");
  #endif
 #endif
#endif


#if NET_LICENCE == TRUE
    if (RunningOnNet(FS_Name) != 0)
        {
        ChangeIndirectedData((char *)&window_1, 6, 5, "network. Please contact:");
        ChangeIndirectedData((char *)&window_2, 6, 5, "network. Please contact:");
        ChangeIndirectedData((char *)&window_3, 7, 5, "network.");
        if ((status = GetIDStatus()) == ID_STATUS__NOT_CONFIGURED)
            {
            Configure_Application();
            ConfigureFS(FS_Name);
            status = GetIDStatus();                                 /* read again as will have changed */
            }
        else SetApplicationRegistrationNumber(&reg, TRUE);

        if (Cmp_FileServerNames(FS_Name) != 0)
            {
            ret_value = SP_ViolationWarningDBox(SP_win_handle);
            }
        }
    else
        {
#endif


#if ((SINGLE_LICENCE == TRUE) || (SITE_LICENCE == TRUE) || (MULTI_LICENCES != -1))
        if ((status = GetIDStatus()) == ID_STATUS__NOT_CONFIGURED)
            {
            Configure_Application();
            status = GetIDStatus();                                 /* read again as will have changed */
            }
        else SetApplicationRegistrationNumber(&reg, TRUE);

        if (status != ID_STATUS__IDENTICAL)
            {
            if (RunningOnMasterDisc() == FALSE)
                {
                ret_value = SP_ViolationWarningDBox(SP_win_handle);
                }
            }
 #if OLD_SYSTEM_SUPPORT
       else OldSyetem_KeyDiscSupport();
 #endif

    /***       NB. if running upon proper "Master Disc" then run regardless of ID number status     ***/

#else
        exit(0);
#endif


#if NET_LICENCE == TRUE
        }
#endif





    /***            Return control back to calling program, as software is now running           ***/
    return(ret_value);
    }












/******************************************************************************/
/***                                                                        ***/
/***  SP_ViolationWarningDBox()                                             ***/
/***                                                                        ***/
/***  This routine outputs a dialogue box, which is defined within the code ***/
/***  so that is can not easily be altered.                                 ***/
/***  The user must then select "O.K." or "CANCEL", if cancel is selected   ***/
/***  the application is aborted, otherwise the application continues with  ***/
/***  the dialogue box on the screen and a Icon on the ICON bar.            ***/
/***                                                                        ***/
/******************************************************************************/

int SP_ViolationWarningDBox(int *SP_win_handle)
    {
    extern int *window_2;
    extern int *window_1;
    extern char LicenceHolder[];
    int win_handle;
    int selection;

    ChangeIndirectedData((char *)&window_2, 17, 0, LicenceHolder);
    ChangeIndirectedData((char *)&window_1, 3, 0, LicenceHolder);
    Open_Window((win_handle = Create_Window((char *) &window_2)));
    Belch();

    selection = Wait_For_Icon_Selection(win_handle, 1, 4, 0);
    Delete_Window(win_handle);
    if (selection == 4) exit(0);

    
    Open_Window((*SP_win_handle = Create_Window((char *) &window_1)));
    return(TRUE);
    }






/******************************************************************************/
/***                                                                        ***/
/***  Configure_Application()                                               ***/
/***                                                                        ***/
/***  This routine configures the ID to the machine which the application   ***/
/***  is running on. A number of dialogue boxes are used to inform the user ***/
/***  what is going on.                                                     ***/
/***                                                                        ***/
/******************************************************************************/


void Configure_Application()
    {
    extern int *window_3;
    extern int *window_4;
    extern int *window_7;
    extern char LicenceHolder[];
    int win_handle, win_handle1;
    int selection;
    int id_value;
#if MASTER_DISC == TRUE
    int MasterDisc_status;
    ID MasterDisc_ID;
    REG_NO MasterDisc_RegNo;
#endif
    ID system_id;
    REG_NO system_RegNo;



#if MASTER_DISC == TRUE
    MasterDisc_status = EnsureMasterDiscInDrive(&MasterDisc_ID, &MasterDisc_RegNo, &id_value);
 #if SINGLE_LICENCE == FALSE
    if ((MasterDisc_status == ID_STATUS__IDENTICAL) && (RunningOnMasterDisc() == TRUE))
        {
        SetApplicationRegistrationNumber(&MasterDisc_RegNo, FALSE);
        return;
        /*   special case for non single user licence, master disc configure once,    */
        /*   running on the master disc (as ID info says not configured).             */
        }
 #endif 
#endif


    ReadSystemIDNumber(&system_id);
    EncodeIDNumbers(&system_id);
    CalculateRegNo(&system_id, &system_RegNo);
#if MASTER_DISC == TRUE
    if (MasterDisc_status == ID_STATUS__NOT_CONFIGURED)
        {
        Open_Window((win_handle = Create_Window((char *) &window_3)));
        Belch();
        if ((selection = Wait_For_Icon_Selection(win_handle, 1, 4, 0)) == 4) exit(0);
        Delete_Window(win_handle);

        if (id_value <= 1)
            {
            SetApplicationRegistrationNumber(&system_RegNo, FALSE);

            win_handle  = Create_Window((char *) &window_4);
            win_handle1 = Create_Window((char *) &window_7);
            while(strlen(LicenceHolder) == 0)
                {
                Open_Window(win_handle);
                selection = Wait_For_Icon_Selection(win_handle, 1, 1, 4);           /* allow user to enter licence holder */
                if (strlen(LicenceHolder) == 0)
                    {
                    Open_Window(win_handle1);
                    Wait_For_Icon_Selection(win_handle, 1, 1, 0);
                    }
                }
                Delete_Window(win_handle);
                Delete_Window(win_handle1);

            if (WriteIDToMasterDisc(&system_id, &system_RegNo, TRUE) == FALSE) IO_Error();
            }
        else
            {
            SetApplicationRegistrationNumber(&MasterDisc_RegNo, FALSE);
            if (WriteIDToMasterDisc(&system_id, &MasterDisc_RegNo, TRUE) == FALSE) IO_Error();
            }
        }
    else
        {
        EncodeIDNumbers(&MasterDisc_ID);
#if ((SINGLE_LICENCE == TRUE) || (MULTI_LICENCES !=-1))
        system_id = MasterDisc_ID;                      /* always write out whats in special track, if single */
                                                        /* else write out whats in the system ID */
#endif
        system_RegNo = MasterDisc_RegNo;                /* always write out whats in special track */
        SetApplicationRegistrationNumber(&system_RegNo, FALSE);
        }
#endif


#if SINGLE_LICENCE == TRUE
    StoreIDNumbersToOriginalDisc(&system_id, &system_RegNo);
#endif
    if (RunningOnMasterDisc() == FALSE)
        if (StoreIDNumbers(&system_id, &system_RegNo) == FALSE)  IO_Error(); 
    }



